package edu.qichen.onlineBlog.framework.utils;

import cn.hutool.core.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

/**
 * 功能:发送邮箱验证码工具类
 * 作者:程序员欧阳子韩
 * 日期:2024/4/6 21:13
 */
@Component
public class MailUtils {
    // 这个是 mail 依赖提供给我们的发送邮件的接口
    @Autowired
    private JavaMailSender mailSender;

    // 获取发件人邮箱
    @Value("${spring.mail.username}")
    private String clientMail;

    private void sendMail(String email) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom('<' + clientMail + '>');
        message.setTo(email);
        message.setSubject("欢迎访问在线考试系统");

        // 使用hutool 生成6位随机数验证码
        String code = RandomUtil.randomNumbers(6);
        String content = "【验证码】您的验证码为：" + code + "。验证码五分钟内有效，逾期作废。\\n\\n\\n" +
                "------------------------------\n\n\n";
        message.setText(content);
        mailSender.send(message);
    }
}
